package com.jason.seckill.service.impl;

import java.math.BigDecimal;
import java.util.Date;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.UpdateChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jason.seckill.exception.GlobalException;
import com.jason.seckill.mapper.OrderMapper;
import com.jason.seckill.pojo.Order;
import com.jason.seckill.pojo.SeckillGoods;
import com.jason.seckill.pojo.SeckillOrder;
import com.jason.seckill.pojo.User;
import com.jason.seckill.service.IGoodsService;
import com.jason.seckill.service.IOrderService;
import com.jason.seckill.service.ISeckillGoodsService;
import com.jason.seckill.service.ISeckillOrderService;
import com.jason.seckill.utils.MD5Util;
import com.jason.seckill.utils.RedisUtil;
import com.jason.seckill.utils.UUIDUtil;
import com.jason.seckill.vo.GoodsVo;
import com.jason.seckill.vo.OrderDetailVo;
import com.jason.seckill.vo.RespBeanEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author jason
 * @since 2021-10-23
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderService {

    @Autowired
    private ISeckillGoodsService seckillGoodsService;
    @Autowired
    private ISeckillOrderService seckillOrderService;
    @Autowired
    private IGoodsService goodsService;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private OrderMapper orderMapper;

    @Transactional
    @Override
    public Order seckill(User user, GoodsVo goods) {
        SeckillGoods seckillGoods =
                seckillGoodsService.getOne(new QueryWrapper<SeckillGoods>()
                        .eq("goods_id", goods.getId()));
        //进行数量-1
        //seckillGoods.setStockCount(seckillGoods.getStockCount() - 1);
        //更新 这里加这段是为了防止超卖
        boolean seckillGoodsResult = seckillGoodsService.update(
                new UpdateWrapper<SeckillGoods>().setSql("stock_count = " + "stock_count-1")
                        .eq("goods_id", goods.getId())
                        .gt("stock_count", 0)
        );
        if (!seckillGoodsResult) {
            return null;
        }
        if (seckillGoods.getStockCount() < 1) {
            //没库存了 存入redis中
            redisUtil.set("isStockEmpty:" + goods.getId(), "0");
            return null;
        }
        //生成订单
        Order order = createOrder(user, goods);
        orderMapper.insert(order);
        //生成秒杀订单 这里其应该写一个方法 封装到seckillOrderService中会比较合理一点
        //下次再说。
        SeckillOrder seckillOrder = createSeckillOrder(user, goods, order);
        redisUtil.set("order" + user.getId() + ":" + goods.getId(), seckillOrder,300);
        seckillOrderService.save(seckillOrder);
        return order;
    }

    private SeckillOrder createSeckillOrder(User user, GoodsVo goods, Order order) {
        SeckillOrder seckillOrder = new SeckillOrder();
        seckillOrder.setGoodsId(goods.getId());
        seckillOrder.setUserId(user.getId());
        seckillOrder.setOrderId(order.getId());
        return seckillOrder;
    }

    private Order createOrder(User user, GoodsVo goods) {
        Order order = new Order();
        order.setUserId(user.getId());
        order.setGoodsId(goods.getId());
        order.setDeliveryAddrId(0L);
        order.setGoodsName(goods.getGoodsName());
        order.setGoodsCount(1);
        order.setGoodsPrice(goods.getGoodsPrice());
        order.setOrderChannel(1);
        //创建未支付
        order.setStatus(0);
        order.setCreateDate(new Date());
        return order;
    }

    @Override
    public OrderDetailVo detail(Long orderId) {
        if (null == orderId) {
            throw new GlobalException(RespBeanEnum.ORDER_NOT_EXIST);
        }
        //查找订单id
        Order order = orderMapper.selectById(orderId);
        //根据订单的商品id查找对应商品
        GoodsVo goodsVo = goodsService.findGoodsVoByGoodsId(order.getGoodsId());
        OrderDetailVo detail = new OrderDetailVo();
        //赋值
        detail.setGoodsVo(goodsVo);
        detail.setOrder(order);
        return detail;
    }

    @Override
    public String createPath(User user, Long goodsId) {
        String str = UUIDUtil.uuid();
        redisUtil.set("seckillPath:"+user.getId()+":"+goodsId,str,60);
        return str;
    }

    @Override
    public boolean checkPath(User user, Long goodsId,String path) {
        String redisPath =(String) redisUtil.get("seckillPath:" + user.getId() + ":" + goodsId);
        return path.equals(redisPath);
    }
}
